package com.douwong.student.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.ListView;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.adapter.ScoreAdapter;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.contant.Config;
import com.douwong.student.model.TestInfo;
import com.douwong.student.utils.JsonUtils;

/**
 * @autor yichao
 * @time 2017/4/716:17
 * @des 课堂考试信息
 */
public class TestInfoActivity extends BaseActivity {
    private TextView getScore,rang,number,totalScore,errorNum;
    private ListView mListView;

    private TestInfo mTestInfo;

    @Override
    protected void initView() {



        setContentView(R.layout.score_layout);
        getScore=(TextView) findViewById(R.id.getScore);
        rang=(TextView) findViewById(R.id.rang);
        number=(TextView) findViewById(R.id.number);
        totalScore=(TextView)findViewById(R.id.totalScore);
        errorNum=(TextView) findViewById(R.id.errorNum);
        mListView= (ListView) findViewById(R.id.score_list);
    }
    IntentFilter mIntentFilter=new IntentFilter(){
        {
            addAction(Config.FINISH_STATISTICS);
            addAction(Config.TEACHER_LEAVE_CLASS);
            addAction(Config.LOGIN_CLASS_FAILURE);
        }
    };
    BroadcastReceiver mReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            switch (intent.getAction()) {
                case Config.FINISH_STATISTICS:
                case Config.TEACHER_LEAVE_CLASS:
                case Config.LOGIN_CLASS_FAILURE:
                    finish();

                    break;

            }

        }
    };

    @Override
    protected void initData() {
        super.initData();
        String stringExtra = getIntent().getStringExtra(Config.INTENT_DATA);
        mTestInfo = JsonUtils.deserialize(stringExtra, TestInfo.class);
        registerReceiver(mReceiver,mIntentFilter);
        if (mTestInfo!=null){
            getScore.setText("得分:\t"+mTestInfo.getGetscore());
            rang.setText("排名:\t"+mTestInfo.getRanking());
            number.setText("人数:\t"+mTestInfo.getStudents());
            errorNum.setText("错题:\t"+mTestInfo.getErrors());
            totalScore.setText("总分:\t"+mTestInfo.getTotal());
            ScoreAdapter scoreAdapter = new ScoreAdapter(this, mTestInfo.getList());
            mListView.setAdapter(scoreAdapter);
        }
    }

    public static void startActivity(Context context, String StrData){
        Intent intent=new Intent(context,TestInfoActivity.class);
        intent.putExtra(Config.INTENT_DATA,StrData);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mReceiver!=null){
            unregisterReceiver(mReceiver);
            mReceiver=null;
        }
    }
}
